﻿namespace FFF.Test.Jobs
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Net.Mail;
    using Abp.BackgroundJobs;
    using Abp.Dependency;
    using Abp.Domain.Uow;
    using Abp.EntityFrameworkCore;
    using Abp.Net.Mail;
    using Microsoft.EntityFrameworkCore;
    using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.Util.Consts;
    using Ydm.Helper;

    /// <summary>
    /// 发送短信
    /// </summary>
    public class SendSsmsJob : BackgroundJob<SendSsmsJobArgs>, ITransientDependency
    {
        private readonly IDbContextProvider<MyDbContext> _dbContextProvider;

        private readonly IEmailSender _emailSender;

        /// <summary>
        /// Initializes a new instance of the <see cref="SendSsmsJob"/> class.
        /// 构造函数
        /// </summary>
        /// <param name="dbContextProvider">数据库访问对象</param>
        /// <param name="emailSender">邮件对象</param>
        public SendSsmsJob(
            IDbContextProvider<MyDbContext> dbContextProvider,
            IEmailSender emailSender)
        {
            this._dbContextProvider = dbContextProvider;
            this._emailSender = emailSender;
        }

        /// <summary>
        /// job执行方法
        /// </summary>
        /// <param name="args">参数</param>
        [UnitOfWork(isolationLevel: System.Transactions.IsolationLevel.ReadUncommitted)]
        public override void Execute(SendSsmsJobArgs args)
        {
            var db = this._dbContextProvider.GetDbContext();
            YdmSSMSHelper.SendAcs(
                AppConfigConsts.SmmsSetting.AccessKeyId,
                AppConfigConsts.SmmsSetting.AccessKeySecret,
                AppConfigConsts.SmmsSetting.SignName,
                args.Phone,
                args.TemplateCode,
                args.TemplateParamJson,
                args.OutId
                );
        }
    }

    /// <summary>
    /// job参数
    /// </summary>
    [Serializable]
    public class SendSsmsJobArgs
    {
        /// <summary>
        /// 电话
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// 发送的模板代码代码
        /// </summary>
        public string TemplateCode { get; set; }

        /// <summary>
        /// 发送的Json串 默认 new { code = ssmCode }
        /// </summary>
        public dynamic TemplateParamJson { get; set; }

        /// <summary>
        /// 业务ID
        /// </summary>
        public string OutId { get; set; } = "FFF.Test";
    }
}
